<template>
  <el-dialog
    :visible.sync="showPass"
    width="400px"
    top="20vh"
    :append-to-body="true"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <div slot="title" class="title_close">
      <!-- 设置登录密码 -->
      <div class="title">{{ $t('inccom.set_password.56hyx4cf7ao0') }}</div>
      <img src="@/assets/img/icon_gb1.png" class="close" @click="hideSetPass(false)" />
    </div>
    <!-- 输入新密码 -->
    <div class="phone_captcha">
      <!-- 密码 -->
      <PasswordInput @passwordChange="(val) => (password1 = val)"  :placeholderContent="$t('inccom.set_password.56hyx4cf87c0')">
      <h2>{{ $t('inccom.set_password.56hyx4cf8280') }}</h2>
      </PasswordInput>
      <PasswordInput @passwordChange="(val) => (password = val)"  :placeholderContent="$t('inccom.set_password.56hyx4cf8as0')">
      <h2>{{ $t('inccom.set_password.56hyx4cf8as0') }}</h2>
      </PasswordInput>
      <button @click="setPassword" class="submit">
        {{ $t("m.determine") }}
      </button>
    </div>
  </el-dialog>
</template>

<script>
import {post_changePwd, post_logout} from '@/assets/js/API/user.js'
export default {
  props: {
    showPass: Boolean,
  },
  data() {
    return {
      lang: "",
      password: "",
      password1: "", // 确认密码
    };
  },
  created() {},
  mounted() {
    this.lang = localStorage.getItem("lang");
    var lang = localStorage.getItem("lang") == "en" ? "en" : "zh-CN";
  },
  methods: {
    async setPassword() {
      if(this.password == "") return this.$message.error(this.$i18n.t("m.inputPsd"));
      if(this.password1 == "") return this.$message.error(this.$i18n.t("m.confirmationPsd"));
      if(this.password1 !== this.password) return this.$message.error(this.$i18n.t("m.twoPasswords"));
      const res = await post_changePwd({
        newPwd: this.password,
        reNewPwd: this.password1
      });
      if(res.code != 1) return this.$message.error(res.msg);
      this.logouts();
      this.hideSetPass(true);
      return this.$message.success(res.msg);
    },
    hideSetPass(flag) {
      this.password = "",
      this.password1 = "",
      this.$emit("closeSet",flag);
    },
    // 退出登录
    async logouts() {
      const res = await post_logout();
      if (res.code != 1) return this.$message.error(res.msg);
      this.$message.success(res.msg);
      sessionStorage.clear();
      this.$router.push(`/login?lang=${this.Lang}`);
      setTimeout(()=> {
        window.location.reload();
      },100)
    },
  },
};
</script>
<style lang="less">
.phone_captcha {
  h2 {
    color: @fbSix;
    font-size: 12px;
    padding-top: 8px;
  }
  .leftfix {
    padding-left: 0;
    padding-right: 0;
    .border {
      // border: 1px solid @fbSix;
      // border-radius: 3px;
      margin-top: 12px;
      .password_input {
        font-size: 12px;
        .close_icon {
          height: 22px;
          margin-top: 10px;
        }
        .el-input__inner {
          border: none;
          background-color: @LightBorderLine;
        }
      }
    }
  }
  .submit {
    width: 100%;
    background-color: @hoverColor;
    height: 40px;
    border-radius: 3px;
    border: 1px solid transparent;
    color: @fff;
    font-size: 12px;
    margin-top: 30px;
  }
}
</style>